import { _decorator, Component, Node, Label, input, Input, EventKeyboard, KeyCode, Vec3, Prefab, director, RigidBody2D, Vec2, instantiate, PolygonCollider2D, Contact2DType, Collider2D, IPhysics2DContact, BoxCollider2D, find, AudioSource } from 'cc';
import { ballController } from './ballControl';
import { audioControl } from './audioControl';
import { heiziControl } from './heiziControl';
import { heiziManager } from './heiziManager';
const { ccclass, property } = _decorator;

@ccclass('Ikun')
export class Ikun extends Component {

  direction: string = 'right'
  isMove: boolean = false
  @property({
    type: Number
  })
  step: number = 200
  @property(Number)
  jumpPower = 10
  isJump: boolean = false

  @property({ type: Prefab })
  preBall: Prefab = null
  audio: audioControl = null

  gameOverUI: Node = null
  score: number = 0
  scoreUI: Label = null
  isDead: boolean = false
  _heiziManager: heiziManager = null

  onLoad() {
    input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
    this.gameOverUI = this.node.getParent().getChildByName('GameOver')
    this.scoreUI = this.node.getParent().getChildByName('userUI').getChildByName('score').getComponent(Label)
    this.scoreUI.string = `Gin天消灭了: ${this.score}只小黑子`
  }
  start() {
    this.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
    this.audio = this.node.getParent().getChildByName('audio').getComponent(audioControl)
    this._heiziManager = this.node.getParent().getChildByName('heiziManage').getComponent(heiziManager)
  }
  update(dt: any) {
    if (this.isMove) {
      const { x, y, z } = this.node.position
      const moveX = this.direction === 'left' ? x - this.step * dt : x + this.step * dt
      this.node.setPosition(new Vec3(moveX, y, z))
    }
  }


  onKeyDown(e: EventKeyboard) {
    if (e.keyCode === KeyCode.ARROW_UP && !this.isJump) {
      this.isJump = true
      this.getComponent(RigidBody2D).linearVelocity = new Vec2(0, this.jumpPower)
    }
    if (e.keyCode === KeyCode.ARROW_LEFT) {
      this.direction = 'left'
      this.node.setScale(new Vec3(1, 1, 1))
      this.isMove = true
    }
    if (e.keyCode === KeyCode.ARROW_RIGHT) {
      this.direction = 'right'
      this.node.setScale(new Vec3(-1, 1, 1))
      this.isMove = true
    }
    if (e.keyCode === KeyCode.SPACE) {
      this.attack()
    }
  }
  onKeyUp(e: EventKeyboard) {
    if (e.keyCode === KeyCode.ARROW_LEFT) {
      this.isMove = false
    }
    if (e.keyCode === KeyCode.ARROW_RIGHT) {
      this.isMove = false
    }
  }
  attack() {
    //实例化 篮球
    const ball = instantiate(this.preBall)
    // 位置
    const { x, y, z } = this.node.position
    ball.setPosition(new Vec3(this.direction === 'left' ? (x - 80) : (x + 80), y - 20, z))
    // 方向
    ball.getComponent(ballController).direction = this.direction
    // 挂载
    this.node.getParent().addChild(ball)
    this.audio.shotVoice()
  }

  //检测是否可以跳跃
  onBeginContact(self: Collider2D, other: Collider2D, contact: IPhysics2DContact) {
    if (other.tag === 2) {
      this.isJump = false
    }
    //撞黑子了
    if (other.tag === 3 && !this.isDead) {
      this.ikundie()
    }
  }
  onDestroy() {
    input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
  }

  ikundie() {
    this.isDead = true
    this.gameOverUI.active = true
    this._heiziManager.isCreateHeizi = false
    this.audio.ikundieVoice()
    this.node.getParent().children.forEach((item: Node) => {
      if (Object.keys(item).indexOf('isMove')) {
        item['isMove'] = false
      }
    })
    this.schedule(()=>{      
      this.node.destroy()
    })
  }
  updateScore() {
    this.score++
    this.scoreUI.string = `Gin天消灭了: ${this.score/2}只小黑子`
  }
}


